//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//
//  Log.cs
//  Created on:      28-feb-2005 0:49:42
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
using System;
using System.IO;

using log4net;
using log4net.Config;

namespace Yawf.Core.Logs {
	/// <summary>
	/// A wrapper class for handling logging with log4net
	/// This class is a Singleton class with several log method to
	/// be used by classes
	/// </summary>
	public class Log {
		// Single instance
		private static readonly Log _instance = new Log();

		/// <summary>
		/// Private constructor, cause this is a Singleton
		/// </summary>
		private Log() {
			FileInfo logFile, confFile;

			try {
				// Check out config file first
				confFile = new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
				if (!confFile.Exists) {
					return;
				}

				// Check first in same directory that config file 
				logFile = new FileInfo(confFile.Directory.FullName + "/log4net.xml");
				if (logFile.Exists) {
					XmlConfigurator.Configure(logFile);
				} else {
					// Check in bin folder(for web apps)
					logFile = new FileInfo(confFile.Directory.FullName + "/bin/log4net.xml");
					if (logFile.Exists) {
						XmlConfigurator.Configure(logFile);
					} else {
						// Check out config file
						XmlConfigurator.Configure();
					}
				}
			} catch (Exception e) {
				System.Console.WriteLine("Exception :" + e);
			}
		}											// LogWrapper.C

		/// <summary>
		/// Gets singelton instance
		/// </summary>
		public Log Instance { get { return _instance; } }

		/// <summary>
		/// Prints a debug message to log, using log4net logger 
		/// </summary>
		/// <param name="classType">Type of class sending log</param>
		/// <param name="logText">Message to write to log</param>
		public static void DebugMessage(Type classType, String logText) {
			ILog logger = LogManager.GetLogger(classType);

			logger.Debug(logText);
		}											// DebugMessage

		/// <summary>
		/// Prints a info message to log, using log4net logger 
		/// </summary>
		/// <param name="classType">Type of class sending log</param>
		/// <param name="logText">Message to write to log</param>
		public static void InfoMessage(Type classType, String logText) {
			ILog logger = LogManager.GetLogger(classType);

			logger.Info(logText);
		}											// InfoMessage

		/// <summary>
		/// Prints a warn message to log, using log4net logger 
		/// </summary>
		/// <param name="classType">Type of class sending log</param>
		/// <param name="logText">Message to write to log</param>
		public static void WarnMessage(Type classType, String logText) {
			ILog logger = LogManager.GetLogger(classType);

			logger.Warn(logText);
		}											// WarnMessage

		/// <summary>
		/// Prints a Error message to log, using log4net logger 
		/// </summary>
		/// <param name="classType">Type of class sending log</param>
		/// <param name="logText">Message to write to log</param>
		public static void ErrorMessage(Type classType, String logText) {
			ILog logger = LogManager.GetLogger(classType);

			logger.Error(logText);
		}											// ErrorMessage

		/// <summary>
		/// Prints a Fatal message to log, using log4net logger 
		/// </summary>
		/// <param name="classType">Type of class sending log</param>
		/// <param name="logText">Message to write to log</param>
		public static void FatalMessage(Type classType, String logText) {
			ILog logger = LogManager.GetLogger(classType);

			logger.Fatal(logText);
		}											// FatalMessage

		/// <summary>
		/// Prints a Fatal message to log, using log4net logger 
		/// </summary>
		/// <param name="classType">Type of class sending log</param>
		/// <param name="logText">Message to write to log</param>
		public static void ExceptionMessage(Type classType, String logText, Exception e) {
			ILog logger = LogManager.GetLogger(classType);

			logger.Error(logText, e);
		}											// ExceptionMessage
	}								// Class LogWrapper
}									// Namespace
